<style scoped>
.site-dropdown {
  display: flex;
  align-items: center;
  padding: 0 8px;
  background-color: #282828;
}
.el-dropdown-link {
  cursor: pointer;
  color: #67c23a;
}
.el-icon-arrow-down {
  font-size: 12px;
}
</style>
<!--  -->
<template>
  <div class="site-dropdown" v-if="currentSite">
    <el-dropdown v-if="currentSite && currentSite.list.length > 1" @command="handleCommand" trigger="click">
      <span class="el-dropdown-link">
        {{ showLanguageByLanguageId(currentSite.site.languageId)
        }}<i class="el-icon-arrow-down el-icon--right"></i>
      </span>
      <el-dropdown-menu slot="dropdown">
        <template v-if="currentSite && currentSite.list.length > 1">
          <el-dropdown-item v-for="(item, index) in currentSite.list" :key="index" :command="item.id">
            {{ showLanguageByLanguageId(item.languageId) }}</el-dropdown-item>
        </template>
      </el-dropdown-menu>
    </el-dropdown>
    <span v-else class="el-dropdown-link" style="font-size: 14px">
      {{ showLanguageByLanguageId(currentSite.site.languageId) }}
    </span>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
export default {
  computed: {
    ...mapGetters([
      'currentSite',
      'alllanguage'
    ])
  },
  data () {
    return {
    }
  },
  methods: {
    handleCommand (command) {
      if (parseInt(this.currentSite.site.id) === parseInt(command)) {
        return
      }
      this.$load('/site/change', { siteId: command }).then((response) => {
        if (response && parseInt(response) === 1) {
          window.location.href = window.location.href
        }
      })
    },
    showLanguageByLanguageId (id) {
      let item = this.alllanguage.find(ele => {
        return parseInt(ele.id) === parseInt(id)
      })
      if (!item) {
        return '未知：' + id
      }
      return item.name
    }
  }
}

</script>
